package com.demo.service;

import java.sql.SQLException;
import java.util.List;

import com.demo.Model.Users;
import com.demo.common.CreateUUID;
import com.demo.daoImpl.DaoImpl;

public class UsersServiceImpl {
	public int save(String id,String loginname) {
		String sql="insert into tab_users(id,loginname) values('"+CreateUUID.getUUID()+"','"+loginname+"')";
		//实例化DaoImpl对象
		DaoImpl dao=new DaoImpl();
		int b=-1;
		try {
			b=dao.three(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return b;
	}
	public int update(Users user) {
		String sql="update  tab_users set loginname='"+user.getLoginname()+"',password='"+user.getPassword()+"',username='"+user.getUsername()+"' where id='"+user.getId()+"'";
		//实例化DaoImpl对象
		DaoImpl dao=new DaoImpl();
		int b=-1;
		try {
			b=dao.three(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return b;
	}
	
	public List<Users> findAll() {
		String sql="select id,loginname,password,username from tab_users";
		DaoImpl dao=new DaoImpl();
		try {
			List<Users> list = dao.find(sql);
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	public Users findById(String id) {
		String sql="select id,loginname,password,username from tab_users where id='"+id+"'";
		DaoImpl dao=new DaoImpl();
		Users user=null;
		try {
			user = dao.findById(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;

	}
	public int delete(String id) {
		String sql="delete from tab_users where id='"+id+"'";
		//实例化DaoImpl对象
		DaoImpl dao=new DaoImpl();
		int b=-1;
		try {
			b=dao.three(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return b;
	}
}
